/**
 * 配置列表展示
 */
var num = 1;
var pageSize = 10;

/**
 * 查询该用户的配置列表
 */
function listConfiguration(pageNum, pageSize){
	$.ajax({
		url: HOST.concat("/protocolConfigs/username?username="+config.username+"&pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token),
		type: "GET",
		success: function(datas){
			checkLogin(datas);
			/*console.log(JSON.stringify(datas));*/
			addContent(datas);
		},
		error: function(){
		    console.log("请求配置列表错误！");
		},
		dataType: "json",
	});
}


/**
 * 删除配置
 */
function deleteConfigurationById(id){
	$.ajax({
		url: HOST.concat("/protocolConfigs/id?token=" + config.token),
		type: "DELETE",
		data: {
		    "id":id
		},
		success: function(datas){
			checkLogin(datas);
			window.location.reload();
		},
		error: function(){
		    console.log("删除配置错误！");
		},
		dataType: "json",
	});
}
/**
 * 批量删除配置
 */
function deleteConfigurations(){
	if(confirm("确认删除所选协议配置吗？")) {
		var ids = ""; //待删除的安全协议配置id数组
		$.each($("#tbody input[type='checkbox']:checked"), function() {
			if(ids == "") {
				ids = ids + $(this).val();
			} else {
				ids = ids + "," + $(this).val();
			}
		});
		console.log(ids);
		$.ajax({
			url: HOST.concat("/protocolConfigs?token=" + config.token),
			type: "DELETE",
			data: {
			    "idList":ids
			},
			success: function(datas){
				checkLogin(datas);
				window.location.reload();
			},
			error: function(){
			    console.log("删除配置错误！");
			},
			dataType: "json",
		});
	}
	
}

function addContent(res){
	//查询成功
	if(res.meta.state==="success") {
	    var list = res.data;
		if(list.length==0){
			alert("已经是最后一页!");
			num--;
		}else{
			console.log("有查询记录");
			console.log(list);
			$("#tbody").empty();
			$("#pageNum").text(num);
			for(var p in list){
				//一条数据
				var item = list[p];
				var id = item["id"];
				var designId = item["designId"];
				var protocolName  = item["protocolName"];
				var designName = item["designName"];
				var createTime = item["createTime"];
				
				var configHtml = '<div class="tr clearfix border-bottom-none">\
											<div class="td w5">\
												<input type="checkbox" value="'+id+'"/>\
											</div>\
											<div class="td w10">C-'+(id.substr(6,2)+id.substr(-6)).toUpperCase()+'</div>\
											<div class="td w20">D-'+(designId.substr(6,2)+designId.substr(-6)).toUpperCase()+'</div>\
											<div class="td w15">'+protocolName+'</div>\
											<div class="td w20">'+designName+'</div>\
											<div class="td w20">'+createTime+'</div>\
											<div class="td w10">\
												<div style="display: inline-block;" class="myIcon">\
													<a href="configurationDetail.html?id='+id+'"><i class="fa fa-search fa-lg" title="查看详情"></i></a>\
												</div>\
												<div style="display: inline-block;" class="myIcon">\
													<a href="#" onclick="deleteConfigurationById(\''+id+'\')">\
														<i class="fa fa-trash fa-lg" title="删除这项配置"></i>\
													</a>\
												</div>\
											</div>\
										</div>';
				$("#tbody").append(configHtml);
			}
		}
		
	}else if(res.meta.message!="notlogin"){
		alert("已经是最后一页!");
		num--;
	}
}

function getConfigurationList(num){
	listConfiguration(num,pageSize)
}

function configLastPage(){
	if(num<=1){
		num = 1;
		alert("已经是第一页!");
		$("#pageNum").text(num);
	}else{
		num--;
		getConfigurationList(num);
	}
}

function configNextPage(){
	num++;
	getConfigurationList(num);
}
function configFirstPage(){
	num = 1;
	$("#pageNum").text(num);
	getConfigurationList(num);
}
getConfigurationList(1);